package com.nbjtjc.safe.mapper.sys;

import com.nbjtjc.safe.model.sys.SysPermission;
import com.nbjtjc.util.MyMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface SysPermissionMapper extends MyMapper<SysPermission> {

    @Select("select * from sys_permission order by order_num asc")
    List<SysPermission> selectAllByOrderNum();
    List<SysPermission> findSubPermissionCount(@Param("validIds") List<Integer> validIds);
    @Select("SELECT " +
            "sys_permission.perms " +
            "FROM " +
            " sys_user_role " +
            "LEFT JOIN sys_role_permission  ON sys_user_role.roleId=sys_role_permission.role_id " +
            "LEFT JOIN sys_permission  ON sys_role_permission.permission_id=sys_permission.id " +
            "WHERE " +
            " sys_user_role.userId = #{userId} " +
            " AND sys_permission.perms is not NULL")
    List<String> findPermissionByUserId(int userId);


    List<SysPermission> findPerByFatherId(@Param("parentId") int parentId,@Param("validIds")List<Integer> validIds);

    @ResultMap("BaseResultMap")
    @Select("SELECT DISTINCT sys_permission.* from sys_user_role ur " +
            "LEFT JOIN sys_role_permission rp ON ur.roleId= rp.role_id " +
            "RIGHT JOIN sys_permission  ON rp.permission_id=sys_permission.id " +
            "WHERE ur.userId=#{userId} and sys_permission.type=#{type}  ORDER BY sys_permission.order_num asc")
    List<SysPermission> findPerByUser(@Param("userId") int userId,@Param("type") int type);


    @Select("SELECT DISTINCT sys_permission.id from sys_user_role ur " +
            "LEFT JOIN sys_role_permission rp ON ur.roleId= rp.role_id " +
            "RIGHT JOIN sys_permission  ON rp.permission_id=sys_permission.id " +
            "WHERE ur.userId=#{userId} ORDER BY sys_permission.order_num asc")
    List<Integer> findPerIdsByUser(@Param("userId") int userId);
}